package com.ruoyi.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateRangeUtil {
    public static List<String> getDatesInRange(String startDateStr, String endDateStr) {
        List<String> dateList = new ArrayList<>();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(startDateStr);
            Date endDate = sdf.parse(endDateStr);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);

            while (!calendar.getTime().after(endDate)) {
                dateList.add(sdf.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateList;
    }

    public static String[] getWeekRange(Date inputDate) {
        String[] weekRange = new String[2];

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inputDate);

        // 获取本周的周一
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        weekRange[0] = sdf.format(calendar.getTime());

        // 获取下周的周日
        calendar.add(Calendar.WEEK_OF_YEAR, 1);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        weekRange[1] = sdf.format(calendar.getTime());

        return weekRange;
    }

    public static void main(String[] args) {
        // 假设当前日期是 2024-02-03
        Date inputDate = new Date();

        String[] weekRange = getWeekRange(inputDate);

        System.out.println("本周开始时间: " + weekRange[0]);
        System.out.println("本周结束时间: " + weekRange[1]);
    }
}
